import pygame
import bullet
from pygame.locals import *

HEIGHT,WIDTH = 1000,1000

class Trank(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        #shape
        self.l = 60
        self.size = (self.l,self.l)
        self.surf = pygame.Surface(self.size)
        self.color = (125,100,160)
        self.surf.fill(self.color)
        self.rect = self.surf.get_rect()
        self.rect.left = WIDTH/2
        self.rect.top = HEIGHT/2
        #atttrubite

        self.speed = 1
        self.dir_x, self.dir_y = 0,-1
        self.moving = 0
        self.level = 1
        self.bullet = bullet.Bullet()
    def shoot(self):
        b = self.l
        a = self.bullet.l
        self.bullet.life = True
        self.bullet.dir_x,self.bullet.dir_y = self.dir_x,self.dir_y
        
        if self.dir_x == 0 and self.dir_y == -1:
            self.bullet.rect.bottom = self.rect.top - 3 # 碰撞的判断，可能会误判，所以+3
            self.bullet.rect.left = self.rect.left + (b-a)//2
        elif self.dir_x == 0 and self.dir_y == 1:
            self.bullet.rect.top = self.rect.bottom + 3
            self.bullet.rect.left = self.rect.left + (b-a)//2
        elif self.dir_x == -1 and self.dir_y == 0:
            self.bullet.rect.bottom = self.rect.bottom - (b-a)//2
            self.bullet.rect.right = self.rect.left - 3
        elif self.dir_x == 1 and self.dir_y == 0:
            self.bullet.rect.bottom = self.rect.bottom - (b-a)//2
            self.bullet.rect.left = self.rect.right + 3
            


        


    def up(self):
        self.dir_x,self.dir_y = 0,-1
    def down(self):
        self.dir_x,self.dir_y = 0,1
    def left(self):
        self.dir_x,self.dir_y = -1,0
    def right(self):
        self.dir_x,self.dir_y = 1,0

    def move(self):

        self.rect.move_ip(self.moving * self.dir_x * self.speed, self.moving*self.dir_y * self.speed)

        
        #限定在屏幕
        if self.rect.left <= 0:
            self.rect.left = 0
        elif self.rect.right >= WIDTH:
            self.rect.right = WIDTH
        if self.rect.top <= 0:
            self.rect.top = 1
        elif self.rect.bottom >= HEIGHT:
            self.rect.bottom = HEIGHT
    def accelerate(self):
        if self.speed < 5:
            self.speed += 1
    def decelerate(self):
        
        if self.speed > 2:
            self.speed -= 1

    def update(self,key):
        #if key[]:
        if key[K_w]:
            self.up()
            self.moving = 1
        if key[K_s]:
            self.down()
            self.moving = 1
        if key[K_a]:
            self.left()
            self.moving = 1
        if key[K_d]:
            self.right()
            self.moving = 1
        if key[K_u]:
            self.accelerate()
            self.bullet.speed = 9
        if key[K_i]:
            self.decelerate()
        if key[K_j] and self.bullet.life == False:
            self.shoot()
        

        if self.bullet.life == True:
            self.bullet.move()
        print("***trank****\n","speed:",self.speed,"moving",self.moving,"dir_x",self.dir_x,"dir_y",self.dir_y)
        
        print("***bullet***\n","speed:",self.bullet.speed,"life:",self.bullet.life)
        self.move()
        
        # 恢复，不然一直移动
        #self.moving = 0
        if self.speed > 0:
            self.moving = 0


